$(function(){
    String.init();
});

var String = {
    init: function() {}
        
    /**
     * 문자열 길이 계산
     */
    lengthCheck: function (data) {
        var count = 0;
        for ( var i=0; i < data.length; i++ ) {
            if( data.charCodeAt(i) < 127 )
                count++;
            else
                count = count + 2;
        }
        return count;
    },

    /**
     * 문자열 자르기
     */
    cutString: function(data,len) {
        var res = '';
        var count = 0;
        for( var i=0; i < data.length; i++ ) {
            count += (data.charCodeAt(i) > 128) ? 2 : 1;
            if(count > len) return data.substr(0,i);
        }

        return data;
    },

    // 문자열의 넓이를 픽셀 단위로 계산
    getStringPixelWidth: function (v){
        var asciiCode;
        var vLength = v.length;
        var character;
        var characterWidth;
        var totalWidth = 0;

        var specialCharZize = 6;
        var multibyteCharSize = 12;
        var baseCharStart = 32;
        var baseCharEnd =  127;
        var asciiCharSize = Array(4,4,4,6,6,10,8,4,5,5,6,6,4,6,4,6,6,6,6,6,6,6,6,6,6,6,4,4,8,6,8,6,12,8,8,9,8,8,7,9,8,3,6,8,7,11,9,9,8,9,8,8,8,8,8,10,8,8,8,6,11,6,6,6,4,7,7,7,7,7,3,7,7,3,3,6,3,11,7,7,7,7,4,7,3,7,6,10,7,7,7,6,6,6,9,6);

        for(i=0; i<vLength; i++){
            character = string_value.substring(i,(i+1));
            asciiCode = character.charCodeAt(0);

            if(asciiCode < baseCharStart){
                characterWidth = specialCharZize;
            }else if(asciiCode <= baseCharEnd){
                idx = asciiCode - baseCharStart;
                characterWidth = asciiCharSize[idx];
            }else if(asciiCode > baseCharEnd){
                characterWidth = multibyteCharSize;
            }
            totalWidth += characterWidth;
        }

        return totalWidth;
    }
};